import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/common/stores/users-store'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      // login
      path: '/login',
      component: () => import('@/views/lgoin/index.vue'),
    },
    {
      path: '/',
      component: () => import('@/layout/backg-default.vue'),
      redirect: '/home',
      children: [
        {
          // 首页 home
          path: '/home',
          component: () => import('@/views/home/index.vue'),
        },
        {
          // 组织 organization
          path: '/organization',
          component: () => import('@/views/organization/index.vue'),
        },
        {
          // 角色 role
          path: '/role',
          component: () => import('@/views/role/index.vue'),
        },
        {
          // 员工 employee
          path: '/information',
          component: () => import('@/views/information/index.vue'),
        },
        {
          // 编辑员工 employee:id?
          path: '/add-employee/:id?',
          component: () => import('@/views/information/add-employee.vue'),
        },
        {
          // 权限 permissions
          path: '/permissions',
          component: () => import('@/views/permissions/index.vue'),
        },
        {
          // 员工考勤 attendance
          path: '/attendance',
          component: () => import('@/views/attendance/index.vue'),
        },
        {
          // 审批 approval
          path: '/approval',
          component: () => import('@/views/approval/index.vue'),
        },
        {
          // 员工工资 wages
          path: '/wages',
          component: () => import('@/views/wages/index.vue'),
        },
        {
          // 工资报表 wages-report
          path: '/wages/report/:yearMonth',
          component: () => import('@/views/wages/wages-report.vue'),
        },
        {
          // 津贴方案 subsidy
          path: '/subsidy',
          component: () => import('@/views/wages/subsidy-steup.vue'),
        },
        {
          // 员工社保 socialsecurity
          path: '/socialsecurity',
          component: () => import('@/views/socialsecurity/index.vue'),
        },
      ],
    },
  ],
})

router.beforeEach((to) => {
  // 如果没有token,且访问的都是登录页，拦截到登录，其他情况放行
  const userStore = useUserStore()
  if (!userStore.userInfo && to.path !== '/login') return '/login'
})

export default router
